package com.version1_3.dao.implement;

import com.SQLGroup.SQLAccess;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.version1_1.dao.UserDao;
import com.version1_3.dao.SharedMethodDao1_3;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kuibu on 15/12/26.
 */
@Repository
public class SharedMethodDaoImpl13 implements SharedMethodDao1_3 {
    private static final Logger LOGGER = Logger.getLogger(SharedMethodDaoImpl13.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserDao userDao;

    /**
     * 获取用户关注分类字符串
     */
    @Override
    public String getUserFocusStr(int userId) {
        return jdbcTemplate.queryForObject(SQLAccess.SelectUserFocusInfoVersion1_3, new Object[]{userId}, java.lang.String.class);
    }

    /**
     * 获取数据库用户关注分类字符串
     *
     * @param userId
     * @return
     */
    @Override
    public String getUserFocusStrAndroid(int userId) {
        return jdbcTemplate.queryForObject(SQLAccess.SelectUserFocusInfo, new Object[]{userId}, java.lang.String.class);
    }

    /**
     * 更新用户关注分类信息
     */
    public boolean updateUserFocus(String userFocusStr, int userId) {
        int rows = jdbcTemplate.update(SQLAccess.UpdateUserFocusInfoVersion1_3, new Object[]{userFocusStr, userId});

        return (rows == 0) ? false : true;
    }

    @Override
    public String getUserTopTypeIds(int userId) {
        return jdbcTemplate.queryForObject(SQLAccess.SelectUserTopTypeIdsVersion1_3, new Object[]{userId}, java.lang.String.class);
    }

    @Override
    public boolean updateUserTopTypeIds(String userTopTypeIds, int userId) {
        int rows = jdbcTemplate.update(SQLAccess.UpdateUserTopTypeIdsVersion1_3, new Object[]{userTopTypeIds, userId});

        return (rows == 0) ? false : true;
    }

    /**
     * TODO: 更新分类订阅量
     *
     * @return
     */
    @Override
    public boolean updateTypeSubscriNumber(JSONArray focusJsonArray, String platform, int userId) {
        List<Integer> addFocusNumberList = new ArrayList<>(); // 需要增加订阅量的分类
        List<Integer> minusFocusNumberList = new ArrayList<>(); // 需要减少订阅量的分类


        if ("Android".equals(platform)) {
            /**
             * 安卓版本接口
             * 格式:{"userId":,"updateFocusList":[{"itemNumber":,"focus":[{"secondType":,"thirdName":""}...]}...]}
             */
            // 用户关注分类信息 - Android版本
            String userFocusStr = getUserFocusStrAndroid(userId);
            JSONObject jsonObject = JSONObject.parseObject(userFocusStr);
            JSONArray userFocusJsonArray = jsonObject.getJSONArray("focusList");
            return userDao.changeThirdFocusNum(focusJsonArray, userFocusJsonArray);

        } else {
            /**
             * iOS版本接口
             * 格式:{"updateFocusStr":[[12,14,20],[14,45],[],[]]}
             */
            // 用户关注分类信息 - iOS版本
            String userFocusStr = getUserFocusStr(userId);
            JSONArray jsonArray = JSON.parseArray(userFocusStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONArray itemJsonArray = jsonArray.getJSONArray(i); // 用户关注分类
                for (int j = 0; j < itemJsonArray.size(); j++) {
                    // 更新分类信息中不包括用户之前的关注分类 - 用户已取消关注原来的分类
                    if (!focusJsonArray.getJSONArray(i).toJSONString().contains(itemJsonArray.getString(j))) {
                        minusFocusNumberList.add(itemJsonArray.getIntValue(j));
                    }
                }
            }

            for (int i = 0; i < focusJsonArray.size(); i++) {
                JSONArray updateItemJsonArray = focusJsonArray.getJSONArray(i);
                for (int j = 0; j < updateItemJsonArray.size(); j++) {
                    // 原来关注分类中不包括更新分类信息中的分类ID
                    if (!jsonArray.getJSONArray(i).contains(updateItemJsonArray.getIntValue(j))) {
                        addFocusNumberList.add(updateItemJsonArray.getIntValue(j));
                    }
                }
            }

            String addTypeIdStr = "";
            for (int typeId : addFocusNumberList) {
                addTypeIdStr = addTypeIdStr.concat(typeId + ",");
            }

            if (addTypeIdStr.isEmpty()) {
                addTypeIdStr = "00";
            }

            String sql = "UPDATE type_info SET collectedNumber=collectedNumber+1 WHERE thirdTypeId IN(" + addTypeIdStr.substring(0, addTypeIdStr.length() - 1) + ")";
            jdbcTemplate.update(sql);

            String minusTypeIdStr = "";
            for (int typeId : minusFocusNumberList) {
                minusTypeIdStr = minusTypeIdStr.concat(typeId + ",");
            }

            if (minusTypeIdStr.isEmpty()) {
                minusTypeIdStr = "00";
            }

            sql = "UPDATE type_info SET collectedNumber=collectedNumber-1 WHERE thirdTypeId IN(" + minusTypeIdStr.substring(0, minusTypeIdStr.length() - 1) + ")";
            jdbcTemplate.update(sql);

            //LOGGER.error("addTypeStr: " + addTypeIdStr + "\nminusTypeStr: " + minusTypeIdStr);


        }

        return true;
    }

}
